﻿namespace CatClient.Message.Spi.Heartbeat
{
    using CatClient.Message.Spi;
    using CatClient.Util;
    using System;
    using System.Diagnostics;
    using System.Runtime.CompilerServices;
    using System.Xml.Serialization;

    [XmlRoot("message")]
    public class MessageInfo : IRefresh
    {
        private readonly Stopwatch _stopwatch;
        private readonly IMessageStatistics _statistics;

        public MessageInfo()
        {
            this._stopwatch = new Stopwatch();
        }

        public MessageInfo(IMessageStatistics statistics) : this()
        {
            this._statistics = statistics;
        }

        public void Refresh()
        {
            this._stopwatch.Restart();
            this.Produced = this._statistics.Produced;
            this.Overflowed = this._statistics.Overflowed;
            this.Bytes = this._statistics.Bytes;
            this._statistics.Reset();
            this._stopwatch.Stop();
            LogInfoWriter.GetInstance("Cat", 0, LogInfoWriter.LogLayout.Default).Info($"Heartbeat {"MessageInfo"} refresh finish. {this._stopwatch.ElapsedMilliseconds}ms");
        }

        [XmlAttribute("produced")]
        public long Produced { get; set; }

        [XmlAttribute("overflowed")]
        public long Overflowed { get; set; }

        [XmlAttribute("bytes")]
        public long Bytes { get; set; }
    }
}

